<?

class systemLog {
	//--------------------------------------------------------------------------------------------------------------	
	var $setup; // table map
	var $info; // current row

	var $table; // table
	var $primary_key; // table primary key
	//--------------------------------------------------------------------------------------------------------------	

	function systemLog($anyDB, $table, $primary_key, $setup){
		$this->anyDB = $anyDB;
		$this->table = $table;
		$this->setup = $setup;
		$this->primary_key = $primary_key;
		return true;
	}
	//--------------------------------------------------------------------------------------------------------------	
	
	function import($info){
		foreach($info as $k=>$v){
			$this->info[$k] = $v;
		}
	}
	//--------------------------------------------------------------------------------------------------------------	
	
	function export(){
		return $this->info;
	}
	//--------------------------------------------------------------------------------------------------------------	

	function get($info){
		$where = array();
		foreach($info as $k=>$v){
			if($v){
				$where[] = $this->setup[$k]."='$v'";
			}
		}
		$query = "SELECT * FROM ".$this->table." WHERE ".implode(" AND ", $where)." ORDER BY ".$this->setup[$this->primary_key]." DESC";
		$this->anyDB->query($query);
		if($this->anyDB->error){
			mail('errors@bastun.net', "System:: get();", $query);
			return false;
		}
		
		return $this->anyDB->getAll();
	}
	//--------------------------------------------------------------------------------------------------------------	

	function add($info){
		$keyz = array();
		$valuez = array();
		foreach($info as $k => $v){
			$keyz[] = $this->setup[$k];
			$valuez[] = "'$v'";
		}
		$query = "BEGIN;INSERT INTO ".$this->table."(".implode(',', $keyz).") VALUES(".implode(',', $valuez).");";
		$this->anyDB->query($query);
		if($this->anyDB->error){
			mail('errors@bastun.net', "System:: add($info);", $query);
			return false;
		}

		$query = "SELECT currval('public.comments_resellers_comment_id_seq') FROM ".$this->table.";";
		$this->anyDB->query($query);
		$res = $this->anyDB->getNext();

		$this->anyDB->query('COMMIT;');

		return $res['currval'];
	}
	//--------------------------------------------------------------------------------------------------------------	

	function update($id, $info){
		foreach($info as $k=>$v){
			if($v){
				$update[] = $this->setup[$k]."='$v'";
			}
		}
		$query = "UPDATE ".$this->table." SET ".implode(',', $update)." WHERE ".$this->setup[$this->primary_key]."=".$id;
		$this->anyDB->query($query);
		if($this->anyDB->error){
			mail('errors@bastun.net', "System:: update($id, $info);", $query);
			return false;
		}
		return true;
	}
	//--------------------------------------------------------------------------------------------------------------	

	function delete($id){
		$query = "DELETE FROM ".$this->table." WHERE ".$this->setup[$this->primary_key]."=".$id;
		$this->anyDB->query($query);
		if($this->anyDB->error){
			mail('errors@bastun.net', "System:: delete($id);", $query);
			return false;
		}
		return true;
	}
	//--------------------------------------------------------------------------------------------------------------	

	function load($id){
		$where[($this->setup[$this->primary_key])] = $id;
		$line = $this->get($where);
		$this->import($line[0]);
	}
	//--------------------------------------------------------------------------------------------------------------	

}

?>